* * * * * ******* ******* ******* ******* ******* ******* ******* ******* 
* * * * * *** Panelize Short Stay Payments
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct
* use medpar_100pct_clean.dta 

* * *** Year month
* drop if mi(date)
* gen year = year(date)
* gen month = month(date)
* gen dt = mofd(mdy(month, 1, year))


* * *** Look at kypho stay changes by day
* * replace group = 999 if inlist(group, 154, 170)
* * preserve
* * keep if group == 999 
* * replace staylength = 30 if staylength > 30 
* * collapse (sum) pmt_amt, by (staylength dt)
* * twoway scatter pmt dt, by(staylength)
* * up to 7 days seem to have changed

* * * *** Restrict to up to 1-week stays
* keep if staylength <= 7

* *Combine treated units
* replace group = 999 if inlist(group, 154, 170)

* * * *** Panelize 
* collapse (sum) pmt_amt, by (group dt)

* * * **** Tsset 
* drop if group == .
* drop if year > 2016
* tsset group dt 
* tsfill, full
* replace pmt_amt = 0 if mi(pmt_amt)

* tsset group dt
* 	* Strong balanced


* * * *** Save panel
* save medpar_100pct_panel.dta, replace 


****************************************************************
**** Find groups that increase similarly to Kyphoplasty over 3 years
cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct
use medpar_100pct_panel.dta, clear

*** Drop groups with too many 0s in the pre-period
gen nonzero = 1 if dt < 551 & pmt > 0 
bysort group: egen nonzeros = total(nonzero) 
drop if nonzeros < 12

*** Drop discontinuous groups
gen disc = pmt/L1.pmt
bysort group: egen maxdisc = max(disc)
drop if maxdisc > 5

** Drop if all 0 in pre-period
bysort group: gen prepmt = pmt_amt if dt < 551 
bysort group: egen prepmttotal = total(prepmt)

**** Look at year series
gen year = year(dofm(dt))
collapse (sum) pmt_amt, by(group year)
drop if mi(year)

*** Panelize by year 
tsset group year 
replace pmt_amt = 0 if mi(pmt_amt)

*** Filing date for Kyphoplasty
*disp mofd(mdy(12, 12, 2005))
*twoway scatter pmt_amt year if group == 999, xline(2005)

* *** Find growth groups over 3 years 
** Need to match this to number of guaranteed pre-periods
gen L3pmt = L3.pmt_amt
gen growth = pmt_amt/L3pmt

* *** Defendant had 2-3x growth starting at < 100M per year 
drop if mi(growth)
drop if year > 2011
sort growth 
keep if growth >= 2


duplicates drop group, force
keep group
drop if group == 999
count
save growthgroups.dta, replace





